﻿using System.Runtime.InteropServices;
using QFramework;
using UnityEngine;

namespace HareGame
{
    public partial class Util
    {
#if UNITY_ANDROID && !UNITY_EDITOR
    private static AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    private static AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    private AndroidJavaObject vibrator = currentActivity.Call<AndroidJavaObject>("getSystemService", "vibrator");
#else
        private static AndroidJavaClass unityPlayer;
        private static AndroidJavaObject currentActivity;
        private AndroidJavaObject vibrator;
#endif

        /// <summary>
        /// 是否启用
        /// </summary>
        public bool IsEnableVibrate
        {
            get
            {
                return GameEntry.GameData.GetBool(UserData.Vibrate, true);
            }
            set
            {
                GameEntry.GameData.SetBool(UserData.Vibrate, value);
            }
        }

        /// <summary>
        /// 震动
        /// </summary>
        /// <param name="vibrateType">强度(可以通过修改枚举中的Int来自定义强度)</param>
        public void Vibrate(VibrateType vibrateType)
        {
            if (GameEntry.GameData.GetBool(UserData.Vibrate, true))
            {
#if UNITY_ANDROID && !UNITY_EDITOR
                vibrator.Call("vibrate", (long)vibrateType);
#elif UNITY_IOS && !UNITY_EDITOR
                if (vibrateType == VibrateType.Tap)
                {
                    _TAG_Unity_iOSTapticSelection();
                }
                else
                {
                    _TAG_iOSNativeVibrate_Vibrate(vibrateType.ToString());
                }
#else 
                LogKit.I("震动:" + vibrateType.ToString());
#endif
            }
        }

#if UNITY_IOS && !UNITY_EDITOR
        [DllImport("__Internal")]
        private static extern void _TAG_Unity_iOSTapticSelection();

        [DllImport("__Internal")]
        private static extern void _TAG_iOSNativeVibrate_Vibrate(string typ);
#endif
    }

    public enum VibrateType
    {
        Tap = 10,
        PEEK = 30,
        POP = 50,
    }
}